package cn.lkp.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import cn.lkp.mall.product.entity.ProductAttrValueEntity;
import cn.lkp.mall.product.service.ProductAttrValueService;
import cn.lkp.mall.product.vo.AttrRespVo;
import cn.lkp.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.lkp.mall.product.entity.AttrEntity;
import cn.lkp.mall.product.service.AttrService;
import cn.lkp.common.utils.PageUtils;
import cn.lkp.common.utils.R;



/**
 * 商品属性
 *
 * @author lkp
 * @email 271309236@qq.com
 * @date 2020-11-11 05:28:02
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 规格参数列表
     * /sale/list/{catelogId}销售属性列表，路径和规格参数列表路径/base/list/{catelogId}类似，
     * 使用动态获取sale或base，合并成一个方法
     * @param params 分页请求参数
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId,@PathVariable("attrType") String attrType){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R attrInfo(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存，原生save方法只会保存属性，不会保存属性分组信息，所以要重写
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R saveAttr(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
    // /product/attr/base/listforspu/{spuId}
    //对应表pms_product_attr_value
    @RequestMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data",entities);
    }
    // /product/attr/update/{spuId}

    /**
     * 请求参数
     * [{
     * 	"attrId": 7,
     * 	"attrName": "入网型号",
     * 	"attrValue": "LIO-AL00",
     * 	"quickShow": 1
     * }, {
     * 	"attrId": 14,
     * 	"attrName": "机身材质工艺",
     * 	"attrValue": "玻璃",
     * 	"quickShow": 0
     * }, {
     * 	"attrId": 16,
     * 	"attrName": "CPU型号",
     * 	"attrValue": "HUAWEI Kirin 980",
     * 	"quickShow": 1
     * }]
     * @param spuId
     * @param entities
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateAttr(@PathVariable("spuId")Long spuId,
                        @RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateAttr(spuId,entities);
        return R.ok();
    }
}
